//
//  SymptomDiseaseViewController.m
//  安好
//
//  Created by rlp on 14-12-5.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SymptomDiseaseViewController.h"
#import "WikiDepartmentViewController.h"
#import "ZhengZhuangViewController.h"

@interface SymptomDiseaseViewController ()

@end

@implementation SymptomDiseaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pages = 1;
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout=UIRectEdgeNone;
    
    if (!self.wikiDepartmentData) {
        self.wikiDepartmentData=[[WikiDepartmentData alloc]init];
    }
    if (!self.symptomDiseaseBaseClass) {
        self.symptomDiseaseBaseClass=[[SymptomDiseaseBaseClass alloc]init];
    }
    [self makeNavBarView];
    [self makeTableView];
    [self loadDataSymptomDisease];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 设置导航条
-(void)makeNavBarView
{
//    self.title = self.wikiDepartmentData.departmentname;
    //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClicked:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
}

-(void)leftButtonClicked:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
    
}
#pragma mark - 创建tableView 设置table的代理、添加头视图横线
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight-iOS7TopMargin)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //创建头视图 - 一条线
    self.headerLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    self.headerLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    self.tableView.tableHeaderView = self.headerLineView;
    [self.tableView addFooterWithTarget:self action:@selector(loadDataSymptomDisease)];

}
#pragma mark - 加载数据 疾病列表
-(void)loadDataSymptomDisease{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)self.pages] forKey:@"page"];
    
    if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[ZhengZhuangViewController class]]){
        [dict setObject:self.symptomid forKey:@"symptomid"];
    }else if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[WikiDepartmentViewController class]]){
        [dict setObject:self.wikiDepartmentData.departmentid forKey:@"departmentid"];
    }

    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,SYMOTOM_DISEASE] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        //用NSNumberFormatter把 NSNumber类型的数字-->转成字符串
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];//得到字符串对象: "0" / "1"
        MyNSLog("%@",responseObject);
        if ([statusString isEqualToString:@"0"]) {
            [self showCustomHudSingleLine:[responseObject objectForKey:@"msg"]];//失败
        }else{
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                
                
                
                self.symptomDiseaseBaseClass = [SymptomDiseaseBaseClass modelObjectWithDictionary:responseObject];
                
                [self.tableView reloadData];
                
                
//                [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

                
//                if (self.symptomDiseaseBaseClass.data==0) {
//                    self.symptomDiseaseBaseClass = [SymptomDiseaseBaseClass modelObjectWithDictionary:responseObject];
//                    
//                }else{
//                    [self.symptomDiseaseBaseClass.data addObjectsFromArray:[(SymptomDiseaseBaseClass*)[SymptomDiseaseBaseClass modelObjectWithDictionary:responseObject] data]];
//                }
                
            }else{
                [self showCustomHudSingleLine:@"网络数据格式错误"];
            }

            
            
            NSArray *jsonDictArr = [self.symptomDiseaseBaseClass.data isEqual:[NSNull null]]?nil:self.symptomDiseaseBaseClass.data;
            
            if (jsonDictArr!=nil) {
                self.pages++;
                
                [self.tableView reloadData];
                
            }else{
                if (self.pages>1) {
                    MyNSLog("下拉无数据");
                }
            }

        }
        [self hiddenActionView];
        [self.tableView footerEndRefreshing];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        [self.tableView footerEndRefreshing];

        
    }];
    
    
}
#pragma mark - tableview Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ((kScreenWidth == 414) ? 44*1.2 : 44);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.symptomDiseaseBaseClass.data count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [(SymptomDiseaseData*)[self.symptomDiseaseBaseClass.data objectAtIndex:indexPath.row] diseasename];
    cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    /**
     *  线条的处理
     */
    
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(5, ((kScreenWidth == 414) ? 44*1.2 : 44)-0.5,kScreenWidth-5, 0.5)];

    
    bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    [cell addSubview:bottomLineView];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    IllnessDetailViewController *illnessDetailViewController = [[IllnessDetailViewController alloc]init];
    illnessDetailViewController.isload = NO;
    illnessDetailViewController.diseaseid = [(SymptomDiseaseData*)[self.symptomDiseaseBaseClass.data objectAtIndex:indexPath.row] diseaseid];
    illnessDetailViewController.diseasename = [(SymptomDiseaseData*)[self.symptomDiseaseBaseClass.data objectAtIndex:indexPath.row] diseasename];
    
    
    [self.navigationController pushViewController:illnessDetailViewController animated:YES];
    
    
    
}

@end
